//
//  YLPundListViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/11/27.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLFundListViewModel.h"
#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
#import "YLAccountTool.h"
#import <YYModel.h>
#import "YLFuncListModel.h"
@implementation YLFundListViewModel

- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        __weak typeof(self) weakSelf = self;
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NSString *url = [NSString stringWithFormat:@"%@Product/PGetFundProductList", YLURL];
                
                NSDictionary *param = input;
                NSInteger index = [param[@"PageIndex"] integerValue];
                
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:input success:^(id responseObject) {
                    
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        
                        YLFuncListItem *item = [YLFuncListItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                        
                        if (index == 1) {
                            [weakSelf.models removeAllObjects];
                        }
                        
                        [weakSelf.models addObjectsFromArray:item.item];
                        
                        
                        
                        [subscriber sendNext:nil];
                        if (item.count > weakSelf.models.count) {
                            
                        } else {
                            [weakSelf.wh_noMoreDataSubject sendNext:nil];
                        }
                        
                    } else {
                        [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                        [WHProgressHUD showError:responseObject[@"message"]];
                        
                    }
                
                    [subscriber sendCompleted];
                    
                } fail:^(NSError *error) {
                    [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    [subscriber sendCompleted];
                
                }];
                
                
                return nil;
            }];
            
        }];
    }
    return _requestCommand;
}

- (NSMutableArray *)models
{
    if (!_models) {
        _models = [NSMutableArray array];
    }
    return _models;
}

@end
